package com.yugao.fintech.antelope.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yugao.fintech.antelope.base.ram.ActionType;
import com.yugao.fintech.antelope.base.ram.RamResource;
import com.yugao.fintech.antelope.system.model.rqrs.article.ArticlePageReq;
import com.yugao.fintech.antelope.system.model.rqrs.article.ArticleResp;
import com.yugao.fintech.antelope.system.model.rqrs.article.ArticleSaveReq;
import com.yugao.fintech.antelope.system.model.rqrs.article.ArticleUpdateReq;
import com.yugao.fintech.antelope.system.service.SysArticleService;
import com.yugao.fintech.framework.core.response.Resp;
import com.yugao.fintech.framework.security.xss.core.XssCleanIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "文章")
@RestController
@RequestMapping("/article")
public class SysArticleController {
    @Autowired
    private SysArticleService articleService;

    @ApiOperation(value = "分页文章")
    @GetMapping("/pageArticle")
    @RamResource(action = "sys:article:query", name = "查询文章", type = ActionType.READ)
    public Resp<IPage<ArticleResp>> pageArticle(@Validated ArticlePageReq req) {
        return Resp.ok(articleService.pageArticle(req));
    }

    @ApiOperation(value = "通过id查找文章")
    @GetMapping("/getArticle")
    @RamResource(action = "sys:article:query", name = "查询文章", type = ActionType.READ)
    public Resp<ArticleResp> getArticle(@RequestParam(value = "id") Long id) {
        return Resp.ok(articleService.getArticle(id));
    }
  
    @ApiOperation(value = "保存文章")
    @PostMapping("/saveArticle")
    @XssCleanIgnore({"content", "title"})
    @RamResource(action = "sys:article:save", name = "保存文章", type = ActionType.WRITE)
    public Resp<?> saveArticle(@Validated @RequestBody ArticleSaveReq req) {
        return Resp.ok(articleService.saveArticle(req));
    }
  
    @ApiOperation(value = "更新文章")
    @PostMapping("/updateArticle")
    @XssCleanIgnore({"content", "title"})
    @RamResource(action = "sys:article:update", name = "更新文章", type = ActionType.WRITE)
    public Resp<?> updateArticle(@Validated @RequestBody ArticleUpdateReq req) {
        return Resp.ok(articleService.updateArticle(req));
    }
  
    @ApiOperation(value = "批量删除文章")
    @DeleteMapping("/batchDeleteArticle")
    @RamResource(action = "sys:article:delete", name = "删除文章", type = ActionType.WRITE)
    public Resp<?> batchDeleteArticle(@RequestParam(value = "ids") List<Long> ids) {
        return Resp.ok(articleService.batchDeleteArticle(ids));
    }
  
    @ApiOperation(value = "删除文章")
    @DeleteMapping("/deleteArticle")
    @RamResource(action = "sys:article:delete", name = "删除文章", type = ActionType.WRITE)
    public Resp<?> deleteArticle(@RequestParam(value = "id") Long id) {
        return Resp.ok(articleService.deleteArticle(id));
    }
}
